﻿using System;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Mimoza.GUI;
using Mimoza.Common;
using Mimoza.ObjectLayer;


namespace SetupWiz
{
    public partial class TerminalName : UserControl,Mimoza.GUI.IWizardPage
    {
        
        private NetworkSettings settings = new NetworkSettings(); 

        public Policy Policy
        {
            get
            {
                HostRecord record = new HostRecord();

                record.NewName = this.textBox1.Text;
                record.AdaptersInfo = settings.AdapterInfo;

                XmlSerializer serializer = new XmlSerializer(typeof(HostRecord));
                TextWriter text = new StringWriter();
                serializer.Serialize(text, record);

                Policy policy = new Policy(null);
                policy.Data = text.ToString();

                return policy;
            }
        }

        #region IWizardPage Members
        private IWizard m_wizard;

        private bool m_IsConfigured = false;

        public void Init(Mimoza.GUI.IWizard wizard)
        {
            m_wizard = wizard;
        }

        public void EnterPage()
        {
            DateTime date = DateTime.Now;
            this.textBox1.Text = "TC-" +
                date.Month.ToString() +
                date.Day.ToString() +
                date.Minute.ToString() +
                date.Second.ToString();
        }

        public bool Try2LeavePage(Mimoza.GUI.WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Forward)
            {
                if (null == this.textBox1.Text || this.textBox1.Text.Length == 0)
                {
                    MessageBox.Show(Resource.Error_EmptyTerminalName, Resource.Error_CaptionInvalidChar, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return false;
                }
                foreach (char c in Resource.InvalidCharacters)
                {
                    if (this.textBox1.Text.Contains(c))
                    {
                        MessageBox.Show(Resource.Error_InvalidCharInName + Resource.InvalidCharacters, Resource.Error_CaptionInvalidChar, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return false;
                    }
                }
                //return true;
            }
            m_IsConfigured = true;
            return true;
        }

        public Mimoza.GUI.IWizard Wizard
        {
            get
            {
                return m_wizard;
            }
        }

        public string PageName
        {
            get { return Resource.TerminalName_PageName; }
        }

        public string PageTitle
        {
            get { return Resource.TerminalName_PageTitle; }
        }

        public bool IsConfigured
        {
            get
            {
                return m_IsConfigured;
            }
            set
            {
                m_IsConfigured = value;
            }
        }

        public event Mimoza.GUI.PageEvent OnPageModified;

        #endregion

        public TerminalName()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            settings.Location = new System.Drawing.Point(this.Parent.Parent.Location.X + (this.Parent.Parent.Width - settings.Size.Width) / 2, this.Parent.Parent.Location.Y + (this.Parent.Parent.Height - settings.Size.Height) / 2);
            settings.ShowDialog();
        }
    }
}
